package com.example.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.Result;
import com.example.entity.User;
import com.example.handlers.BaseContext;
import com.example.service.UserService;
import com.example.vo.BlogSettingVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/blog/setting")
public class SettingController {

    @Resource
    private UserService userService;

    //todo: 获取博客设置
    @PostMapping("/detail")
    public Result<BlogSettingVO> detail() {
        BlogSettingVO blogSettingVO = new BlogSettingVO();

        Integer adminId = BaseContext.getCurrentId();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getAdminId, adminId);
        User user = userService.getOne(wrapper);

        if(user==null){
            user = userService.getById(1);
            blogSettingVO.setLogo(user.getAvatar());
            blogSettingVO.setName(user.getNickname() + "的博客");
            blogSettingVO.setAuthor(user.getNickname());
            blogSettingVO.setIntroduction(user.getIntroduction());
            blogSettingVO.setAvatar(user.getAvatar());
            blogSettingVO.setGithubHomepage(user.getGithubHomepage());
            blogSettingVO.setCsdnHomepage(user.getCsdnHomepage());
            blogSettingVO.setGiteeHomepage(user.getGiteeHomepage());
            blogSettingVO.setZhihuHomepage(user.getZhihuHomepage());

            BeanUtil.copyProperties(user, blogSettingVO);
        }else{
            blogSettingVO.setLogo(user.getAvatar());
            blogSettingVO.setName(user.getNickname() + "的博客");
            blogSettingVO.setAuthor(user.getNickname());
            blogSettingVO.setIntroduction(user.getIntroduction());
            blogSettingVO.setAvatar(user.getAvatar());
            blogSettingVO.setGithubHomepage(user.getGithubHomepage());
            blogSettingVO.setCsdnHomepage(user.getCsdnHomepage());
            blogSettingVO.setGiteeHomepage(user.getGiteeHomepage());
            blogSettingVO.setZhihuHomepage(user.getZhihuHomepage());
        }


        return Result.success(blogSettingVO);
    }


}
